package com.oaker.scheduled.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.oaker.hours.doman.columns.Columns;
import com.oaker.hours.doman.dto.UserHourSaveDTO;
import com.oaker.hours.doman.entity.ProjectHour;
import com.oaker.hours.doman.entity.SysPersonTrend;
import com.oaker.hours.service.MhUserHourMissService;
import com.oaker.hours.service.impl.MhHourDetailServiceImpl;
import com.oaker.hours.service.impl.MhUserHourServiceImpl;
import com.oaker.hours.service.impl.SysPersonTrendServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description :
 * <功能详细描述>
 * @author: 须尽欢_____
 * @Data : 2021/9/15 17:31
 */
@Component("userHourMiss")
@Slf4j
public class UserHourMissTask {

    @Resource
    private MhUserHourMissService userHourMissService;

    @Resource
    private MhUserHourServiceImpl userHourService;

    @Resource
    private SysPersonTrendServiceImpl personTrendService;

    public void userHourMissTask() {
        System.out.println("定时任务被调用了...");
        userHourMissService.userMissTask();
    }

    public void reportTreandTask() {
        log.info("--------------------------------------------------------------");
        log.info("提交当天工时定时任务被调用了...");
        EntityWrapper<SysPersonTrend> queryWrapper = new EntityWrapper<>();
        queryWrapper.lt(Columns.PersonTrend.date,new Date());
        List<SysPersonTrend> spts = personTrendService.selectList(queryWrapper);
        for (SysPersonTrend spt : spts) {
            log.info("开始处理:"+spt.toString());
            UserHourSaveDTO userHourSaveDTO = new UserHourSaveDTO();
            userHourSaveDTO.setUserId(spt.getUserId());
            userHourSaveDTO.setDate(spt.getDate());

            List<UserHourSaveDTO.ProjectHour> list = new ArrayList<>();
            UserHourSaveDTO.ProjectHour projectHour = new UserHourSaveDTO.ProjectHour();
            projectHour.setProjectId(spt.getProjectId());
            projectHour.setHour(new BigDecimal(8));
            list.add(projectHour);

            userHourSaveDTO.setProjectHours(list);
            userHourService.create(userHourSaveDTO);
            log.info("添加工时成功。开始删除："+spt.getTrendId());
            personTrendService.delete(new EntityWrapper<SysPersonTrend>()
            .eq(Columns.PersonTrend.trend_id,spt.getTrendId()));
            log.info("删除成功");
        }

    }


}
